// pages/confirm-payment/confirm-payment.js
Page({
  data: {
    paymentInfo: {
      feeType: '西药费',
      department: '消化内科',
      doctor: '李梅',
      patient: '牛韦',
      cardNumber: '610*********0989'
    },
    feeItems: [
      {
        id: 1,
        name: '普罗布考片',
        unitPrice: '50.00',
        quantity: 3,
        total: '150.00'
      },
      {
        id: 2,
        name: '阿司匹林肠溶片',
        unitPrice: '50.00',
        quantity: 3,
        total: '150.00'
      }
    ],
    paymentMethod: '余额',
    cardBalance: '4100.00',
    totalAmount: '600.00'
  },

  onLoad(options) {
    console.log('确认缴费信息页面加载');
    
    // 根据传入的参数动态设置数据
    if (options.itemName) {
      this.setData({
        'paymentInfo.feeType': options.itemName
      });
    }
    
    // 根据不同的费用类型设置不同的明细和计算合计
    this.setFeeItemsByType(options.itemName);
  },

  // 根据费用类型设置费用明细
  setFeeItemsByType(feeType) {
    const feeItemsMap = {
      '西药费': {
        doctor: '刘美',
        items: [
          {
            id: 1,
            name: '普罗布考片',
            unitPrice: '50.00',
            quantity: 3,
            total: '150.00'
          },
          {
            id: 2,
            name: '阿司匹林肠溶片',
            unitPrice: '50.00',
            quantity: 3,
            total: '150.00'
          }
        ]
      },
      '检验费': {
        doctor: '王医生',
        items: [
          {
            id: 1,
            name: '血常规检查',
            unitPrice: '80.00',
            quantity: 1,
            total: '80.00'
          },
          {
            id: 2,
            name: '生化全套',
            unitPrice: '120.00',
            quantity: 1,
            total: '120.00'
          }
        ]
      },
      '检查费': {
        doctor: '张医生',
        items: [
          {
            id: 1,
            name: 'B超检查',
            unitPrice: '100.00',
            quantity: 1,
            total: '100.00'
          },
          {
            id: 2,
            name: '心电图',
            unitPrice: '50.00',
            quantity: 1,
            total: '50.00'
          }
        ]
      }
    };

    if (feeItemsMap[feeType]) {
      const feeData = feeItemsMap[feeType];
      const feeItems = feeData.items;
      // 计算合计金额
      const totalAmount = feeItems.reduce((sum, item) => {
        return sum + parseFloat(item.total);
      }, 0);
      
      this.setData({
        feeItems: feeItems,
        totalAmount: totalAmount.toFixed(2),
        'paymentInfo.doctor': feeData.doctor
      });
    }
  },

  // 去充值
  goToRecharge() {
    wx.navigateTo({
      			url: '/pages/2411Ayiliao-pages/recharge/outpatient'
    });
  },

  // 确认缴费
  confirmPayment() {
    wx.showLoading({
      title: '缴费中...'
    });

    // 模拟缴费过程
    setTimeout(() => {
      wx.hideLoading();
      
      // 准备缴费信息
      const paymentInfo = {
        feeType: this.data.paymentInfo.feeType,
        department: this.data.paymentInfo.department,
        doctor: this.data.paymentInfo.doctor,
        patient: this.data.paymentInfo.patient,
        cardNumber: this.data.paymentInfo.cardNumber,
        feeItems: this.data.feeItems,
        totalAmount: this.data.totalAmount
      };
      
      // 跳转到缴费成功页面
      wx.navigateTo({
        url: `/pages/payment-success/payment-success?paymentInfo=${JSON.stringify(paymentInfo)}`
      });
    }, 2000);
  }
});
